package com.iflytek.iot.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;

/**
 * @author yu 2019/2/13.
 */
public class JsonUtils {

    private static ValueFilter filter = (obj, s, v) -> {
        if (v == null || "null".equals(v)) {
            return "";
        }
        return v;
    };


    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, filter, SerializerFeature.WriteMapNullValue);
    }


    /**
     * 验证是否是json
     * @param str
     * @return
     */
    public final static boolean isJSONValid(String str) {
        try {
            JSONObject.parseObject(str);
            return true;
        } catch (JSONException ex) {
            try {
                JSONObject.parseArray(str);
                return true;
            } catch (JSONException ex1) {
                return false;
            }
        }
    }

}
